#ifndef __COM_H
#define __COM_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "sys.h"
#include <stdint.h>

#if ENABLE_OS_SUPPORT
#include "FreeRTOS.h"
#include "semphr.h"
#endif

#define COM_TX_BUFFER_SIZE 1024
#define COM_RX_BUFFER_SIZE 1009

    typedef void (*com_rx_callback_t)(void);

    typedef struct Communication
    {
        uint8_t payload[COM_RX_BUFFER_SIZE];
#if ENABLE_OS_SUPPORT
        SemaphoreHandle_t rx_complete;
        SemaphoreHandle_t tx_complete;
#else
    volatile uint8_t rx_complete;
    volatile uint8_t tx_complete;
#endif
        uint16_t size;
        UART_HandleTypeDef *uart;
        com_rx_callback_t callback;
    } com_t;

    extern com_t com_extern, com_internal, com_subctrl;

    void ComExtern_Init(UART_HandleTypeDef *huart, com_rx_callback_t callback);
    void ComExtern_Run();
    void ComExtern_Print(const char *fmt, ...);
    void ComExtern_Send(uint8_t *data, uint16_t size);

    void ComInternal_Init(UART_HandleTypeDef *huart, com_rx_callback_t callback);
    void ComInternal_Run();
    void ComInternal_Send(uint8_t *data, uint16_t size);

    void ComSubCtrl_Init(UART_HandleTypeDef *huart, com_rx_callback_t callback);
    void ComSubCtrl_Run();
    void ComSubCtrl_Send(uint8_t *data, uint16_t size);

#ifdef __cplusplus
}
#endif

#endif